import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/common/index.dart';
import 'package:flutter_shop/model/address_bean.dart';
import 'package:flutter_shop/provide/address_provider.dart';
import 'package:provider/provider.dart';

class AddAddressPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _AddAddressPageState();
  }
}

class _AddAddressPageState extends State<AddAddressPage> {
  TextEditingController _controller;

  @override
  void initState() {
    _controller = TextEditingController();
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  AddressBean addressBean = AddressBean.empty();

  @override
  Widget build(BuildContext context) {
    return Consumer(
      builder: (BuildContext context, AddressProvider val, Widget child) {
        return Scaffold(
          appBar: AppBar(
            leading: IconButton(
              icon: Icon(Icons.arrow_back_ios),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            centerTitle: true,
            title: Text('添加地址'),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.save),
                onPressed: () async {
                  if (addressBean.infoCorrect()) {
                    await val.add(addressBean);
                    Navigator.pop(context);
                  } else {
                    TopReminder.error(context, '请填写相关信息');
                  }
                },
              ),
            ],
          ),
          body: Container(
            padding: EdgeInsets.symmetric(horizontal: 10),
            child: Column(
              children: <Widget>[
                TextField(
                  controller: _controller,
                  onChanged: (str) {
                    addressBean.name = str.trim();
                  },
                  decoration: InputDecoration(hintText: '输入姓名'),
                ),
                TextField(
                  onChanged: (str) {
                    addressBean.phone = str.trim();
                  },
                  decoration: InputDecoration(hintText: '输入电话'),
                ),
                TextField(
                  onChanged: (str) {
                    addressBean.address = str.trim();
                  },
                  decoration: InputDecoration(hintText: '输入地址'),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
